package com.p2p.backend.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.p2p.backend.service.OplogService;
import com.p2p.backend.service.PrivilegeService;
import com.p2p.backend.util.PageUtils;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Oplog;
import com.p2p.base.page.PageVo;
import com.p2p.base.web.controller.BaseController;

/**
 * leiwen@yiqihao.com
 */
@Controller
@RequestMapping("privilege")
public class PrivilegeController extends BaseController {

	@Resource
	private PrivilegeService privilegeService;

	@PermissionNeed("privilege_list")
	@RequestMapping("list")
	public void list(PageVo vo,Model model) {
		model.addAttribute("res", 1);
		PageVo page = privilegeService.listPage(vo);
		model.addAttribute("data", PageUtils.data(page));
	}

	@PermissionNeed("privilege_management")
	@RequestMapping("doadd")
	public void doadd(@RequestParam Map<String, Object> params, Model model, HttpServletRequest request) {
		privilegeService.add(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_PRIVILEGE);
		oplog.setAction(Action.ADD);
		oplog.setParams("");
		OplogService.log(oplog);
	}

	@PermissionNeed("privilege_management")
	@RequestMapping("add")
	public String add(Model model) {
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		model.addAttribute("privilegeList", privilegeList);
		return "privilege/add";
	}

	@PermissionNeed("privilege_management")
	@RequestMapping("edit")
	public String edit(@RequestParam Integer acl, Model model) {
		Map<String, Object> privilege = privilegeService.getDetail(acl);
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		model.addAttribute("privilegeList", privilegeList);
		model.addAttribute("privilege", privilege);
		return "privilege/edit";
	}

	@PermissionNeed("privilege_management")
	@RequestMapping("doupdate")
	public void doupdate(@RequestParam Map<String, Object> params, Model model, HttpServletRequest request) {
		privilegeService.update(params);
		model.addAttribute("res", 1);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_PRIVILEGE);
		oplog.setAction(Action.UPDATE);
		oplog.setParams(ToStringBuilder.reflectionToString(params));
		OplogService.log(oplog);
	}

	@PermissionNeed("privilege_management")
	@RequestMapping("delete")
	public void delete(Integer acl, Model model, HttpServletRequest request) {
		boolean succeed = privilegeService.delete(acl);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.PRIVILEGE_PRIVILEGE);
		oplog.setAction(Action.ADD);
		oplog.setParams("acl="+acl);
	
		if (succeed) {
			model.addAttribute("res", 1);
			oplog.setRemark("删除成功");
		} else {
			model.addAttribute("res", 0);
			model.addAttribute("msg", "该权限下有子权限 或 已经有角色关联了此权限");
			oplog.setRemark("删除失败");
		}
		OplogService.log(oplog);
	}
}
